package oct2013.linkednode;

public class ReverseLinkedListII {
	public ListNode reverseBetween(ListNode head, int m, int n) {
		ListNode dummy = new ListNode(0);
		dummy.next = head;
		ListNode prev = dummy;
		while (m > 1) {
			prev = prev.next;
			m--;
			n--;
		}
		ListNode revCur = prev.next.next;
		ListNode revHead = prev.next;
		ListNode revTail = revHead;
		while (n > m) {
			revTail.next = revCur.next;
			revCur.next = revHead;
			revHead = revCur;
			revCur = revTail.next;
			n--;
		}
		prev.next = revHead;
		return dummy.next;
	}
}
